/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.business.service.impl;

import me.zhengjie.modules.business.domain.FlowExperimentLinkDO;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.business.repository.FlowExperimentLinkRepository;
import me.zhengjie.modules.business.service.FlowExperimentLinkService;
import me.zhengjie.modules.business.service.dto.FlowExperimentLinkDto;
import me.zhengjie.modules.business.service.dto.FlowExperimentLinkQueryCriteria;
import me.zhengjie.modules.business.service.mapstruct.FlowExperimentLinkMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ljj
* @date 2021-03-01
**/
@Service
@RequiredArgsConstructor
public class FlowExperimentLinkServiceImpl implements FlowExperimentLinkService {

    private final FlowExperimentLinkRepository flowExperimentLinkRepository;
    private final FlowExperimentLinkMapper flowExperimentLinkMapper;

    @Override
    public Map<String,Object> queryAll(FlowExperimentLinkQueryCriteria criteria, Pageable pageable){
        Page<FlowExperimentLinkDO> page = flowExperimentLinkRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(flowExperimentLinkMapper::toDto));
    }

    @Override
    public List<FlowExperimentLinkDto> queryAll(FlowExperimentLinkQueryCriteria criteria){
        return flowExperimentLinkMapper.toDto(flowExperimentLinkRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FlowExperimentLinkDto findById(Integer id) {
        FlowExperimentLinkDO flowExperimentLink = flowExperimentLinkRepository.findById(id).orElseGet(FlowExperimentLinkDO::new);
        ValidationUtil.isNull(flowExperimentLink.getId(),"FlowExperimentLink","id",id);
        return flowExperimentLinkMapper.toDto(flowExperimentLink);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FlowExperimentLinkDto create(FlowExperimentLinkDO resources) {
        return flowExperimentLinkMapper.toDto(flowExperimentLinkRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FlowExperimentLinkDO resources) {
        FlowExperimentLinkDO flowExperimentLink = flowExperimentLinkRepository.findById(resources.getId()).orElseGet(FlowExperimentLinkDO::new);
        ValidationUtil.isNull( flowExperimentLink.getId(),"FlowExperimentLink","id",resources.getId());
        flowExperimentLink.copy(resources);
        flowExperimentLinkRepository.save(flowExperimentLink);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            flowExperimentLinkRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FlowExperimentLinkDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FlowExperimentLinkDto flowExperimentLink : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("流量层ID", flowExperimentLink.getFlowId());
            map.put("应用ID", flowExperimentLink.getAppId());
            map.put("实验ID", flowExperimentLink.getExperimentId());
            map.put("实验所占流量比", flowExperimentLink.getRate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}